export default {
    name: 'make_exercise',
    tempdata: {},
    data() {
        return this.tempdata
    },
    created(data) {
        this.makeGen(data);
    },
    methods: {
        getIndex(index) {
            return index ++
        },
        makeGen(data) {
            this.tempdata = data
            let _obj;
            while (this.tempdata.tempdata.tableData.length < this.tempdata.maxNumber) {
                _obj = this.add();
                // _obj = this.sub();
                if (_obj !== undefined) this.tempdata.tableData.push(_obj);
            }
        },
        // 按范围生成随机数
        randomNum() {
            return Math.round(Math.random() * this.tempdata.range);
        },
        // 生成加法数
        add() {
            let _a = this.randomNum();
            let _b = this.randomNum();
            let _temp = {};
            if (_a + _b > this.tempdata.maxNumber) {
                this.add();
            } else {
                _temp = {
                    index: this.tempdata.index ++,
                    number: this.tempdata.questionNum ++,
                    exercise: _a + ' + ' + _b + ' = ',
                    answer: _a + _b
                };
                return _temp;
            }
        },
        sub() {
            let _a = this.randomNum();
            let _b = this.randomNum();
            let _temp = {
                index: this.tempdata.index ++,
                a: _a,
                b: _b,
                result: _a - _b
            };
            if (_a < _b) {
                _temp.a = _b;
                _temp.b = _a;
                _temp.result = _b - _a;
            }
            return _temp;
        },
        checkAnswer(index){
            let p = this.tempdata.tableData[index].answer
            window.alert("你的结果：" + this.tempdata.inputNum[index] + "\n正确答案：" + p)
        }
    }
}
